# -*- coding: utf-8 -*-
import scrapy
import json
from douyucdn.items import DouyucdnItem

class Douyu01Spider(scrapy.Spider):
    name = 'douyu01'
    allowed_domains = ['capi.douyucdn.cn']
    baseUrl = "http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset="
    offset = 0
    limit = 20
    requestUrl = baseUrl + str(offset)
    start_urls = [requestUrl]

    def parse(self, response):
        data_list = json.loads(response.body)["data"]
        if not len(data_list):
            print("抓取完毕!")
            return

        for data in data_list:
            item = DouyucdnItem()
            item["nickname"] = data["nickname"]
            item["vertical_src"] = data["vertical_src"]
            yield item

        self.offset += self.limit
        self.requestUrl = self.baseUrl + str(self.offset)
        yield scrapy.Request(self.requestUrl,callback=self.parse)
